package org.workplicity.marist.grounds.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.workplicity.entry.WorkSlate;
import org.workplicity.entry.WorkRequest;
import org.workplicity.entry.WorkRequest.Status;
import org.workplicity.task.NetTask;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;
import org.workplicity.entry.event.Event;

/**
 *
 * @author Josh Waters and Josh Dixon 
 *
 * This program will analyze the repositories based on the selected
 * criteria passed in from the view- GauntletUI
 * Last Edit 4/25
 */
public class Purge {

    /**
     * This is the worklet context to utilize the Helper pattern.
     */
    private static WorkletContext context = WorkletContext.getInstance();

    /**
     * This method checks for any requests that are not in OPEN/CLOSED
     * status and are not on a workslate
     * @param workRequests is an array list of the work requests
     * @param workSlates is an array list of the work slates
     * @return a list of work requests to be purged
     */
    public static ArrayList<WorkRequest> getRequestsNotOnWorkslate(
            ArrayList<WorkRequest> workRequests, ArrayList<WorkSlate> workSlates) {

        //Arraylist that contains the requests to be purged
        ArrayList<WorkRequest> workRequestsToBePurged = new ArrayList<WorkRequest>();
        //Boolean to determine if the request is on a work slate
        boolean onWorkSlate = false;
        //Get the status of the current request
        Status requestStatus;

        boolean foundRequest = false;
        /*For each Work Request check if Status is closed or open then check
         * if the request is not on a work slate
         */
        for (WorkRequest request : workRequests) {

            //Get the status of the current request
            requestStatus = request.getStatus();

            //Ensure onWorkSlate is false before each loop
            onWorkSlate = false;

            if (requestStatus != Status.CLOSED) {
                if (requestStatus != Status.OPEN) {
                    /* For each work slate, deteremine if the current request
                     * is in the WorkRequestList
                     */
                    for (WorkSlate workSlate : workSlates) {
                        List<Integer> requestList = workSlate.getWorkRequestList();
                        for (int requestID : requestList) {
                            if (requestID == request.getId()) {
                                onWorkSlate = true;
                            }
                        }
                    }


                    if (!onWorkSlate) {
                        workRequestsToBePurged.add(request);
                        foundRequest = true;
                        Report.add(Report.alertType.requestsNotOnWorkSlate, "Work Request ID: " + request.getId() + " Status: "
                                + request.getStatus().name() + "\n");
                    }
                }
            }
        }
        if (!foundRequest) {
            Report.add(Report.alertType.requestsNotOnWorkSlate);
        }
        return workRequestsToBePurged;
    }

    /**
     * This method gets closed requests based on the olderThan criteria selected
     * by the user.
     * @param olderThan is a long representing 1 day, 1 week, 1 month, or 1 year
     * @param workRequests workRequests is an array list of Work Requests
     * @return a list of work requests to  be purged
     */
    public static ArrayList<WorkRequest> getOldClosedRequests(long startDate, long endDate,
            ArrayList<WorkRequest> workRequests) {

        //Contains work requests that meet the critera
        ArrayList<WorkRequest> oldWorkRequestsToBePurged = new ArrayList<WorkRequest>();


        //Used to format dates when using println
        SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");


        /*For each Work Request check if Status is closed then check
         * if the createDate is older than the current time minus the
         * olderThan variable
         */
        boolean foundRequest = false;
        for (WorkRequest request : workRequests) {
            if (request.getStatus() == Status.CLOSED) {
                // System.out.println("Create date" + formatter.format(request.getCreateDate().getTime()));
                if (startDate <= request.getUpdateDate().getTime() && request.getUpdateDate().getTime() <= endDate) {
                    //The request is closed and older than criteria
                    oldWorkRequestsToBePurged.add(request);
                    Report.add(Report.alertType.oldClosedRequests, "Work Request ID: " + request.getId() + " Status: "
                            + request.getStatus().name() + " Create Date: " + formatter.format(request.getUpdateDate().getTime()) + "\n");
                    foundRequest = true;
                }
            }


        }
        if (!foundRequest) {
            Report.add(Report.alertType.oldClosedRequests);
        }
        return oldWorkRequestsToBePurged;
    }

    /**
     * This method will search repositories for entries not in a template class
     */
    public static ArrayList<Event> getFuturesWithInvalidTemplate(ArrayList<Event> futureEvents) {
        //contains events that have null templates
        ArrayList<Event> eventsToBePurged = new ArrayList<Event>();
        boolean foundEvent = false;

        for (Event event : futureEvents) {
            if (event.getTemplate() == null) {
                eventsToBePurged.add(event);
                Report.add(Report.alertType.futuresWithInvalidTemplate, "Event ID: " + event.getId() + "\n");
                foundEvent = true;

            }

        }
        if (!foundEvent) {
            Report.add(Report.alertType.futuresWithInvalidTemplate);
        }
        return eventsToBePurged;
        /*
         * Temp code
         */
    }

    /**
     *This method will get future events that are disabled and expired
     * @param olderThan is a long representing 1 day, 1 week, 1 month, or 1 year
     * @param futureEvents is an array list of future events
     * @return a list of events to be purged
     */
    public static ArrayList<Event> getOldFutureEvents(long startDate, long endDate,
            ArrayList<Event> futureEvents) {

        //Contains events that meet the criteria
        ArrayList<Event> eventsToBePurged = new ArrayList<Event>();




        /*For each events check if it is disabled then check
         * if the createDate is older than the current time minus the
         * olderThan variable
         */
        boolean foundEvent = false;
        for (Event event : futureEvents) {
            if (!event.isEnabled()) {
                if (startDate < event.getTemplate().getUpdateDate().getTime() && event.getTemplate().getUpdateDate().getTime() < endDate) {
                    eventsToBePurged.add(event);
                    Report.add(Report.alertType.oldFutureEvents, "Event ID: " + event.getId() + "\n");
                    foundEvent = true;
                }
            }


        }
        if (!foundEvent) {
            Report.add(Report.alertType.oldFutureEvents);
        }
        return eventsToBePurged;

    }

    /**
     *This method purges the repository of requests not on a workslate
     * @param purgeRequests is an array list of work requests
     */
    public static void purgeRequestsNotOnWorkSlate(ArrayList<WorkRequest> purgeRequests) {
        for (WorkRequest requestToBeDeleted : purgeRequests) {
            if (!Helper.delete(requestToBeDeleted, NetTask.REPOS_CLEARINGHOUSE, context)) {
                String msg = "Delete failed to remove request id = " + requestToBeDeleted.getId() + "!";
                return;
            }
        }
    }

    /**
     *This method purges the repository of old and closed requests
     * @param purgeRequests is an array list of work requests
     */
    public static void purgeOldClosedRequests(ArrayList<WorkRequest> purgeRequests) {
        for (WorkRequest requestToBeDeleted : purgeRequests) {
            if (!Helper.delete(requestToBeDeleted, NetTask.REPOS_CLEARINGHOUSE, context)) {
                String msg = "Delete failed to remove request id = " + requestToBeDeleted.getId() + "!";
                return;
            }
        }
    }

    /**
     *This method purges the repository of futures with an invalid template
     * @param purgeEvents is an array list of events
     */
    public static void purgeFuturesWithInvalidTemplate(ArrayList<Event> purgeEvents) {
        for (Event eventToBeDeleted : purgeEvents) {
            if (!Helper.delete(eventToBeDeleted, NetTask.REPOS_FUTURES, context)) {
                String msg = "Delete failed to remove future id = " + eventToBeDeleted.getId() + "!";
                return;
            }
        }
    }

    /**
     *This method purges the repository of futures that are old
     * @param purgeEvents is an array list of events
     */
    public static void purgeOldFutureEvents(ArrayList<Event> purgeEvents) {
        for (Event eventToBeDeleted : purgeEvents) {

            if (!Helper.delete(eventToBeDeleted, NetTask.REPOS_FUTURES, context)) {
                String msg = "Delete failed to remove future id = " + eventToBeDeleted.getId() + "!";
                return;
            }
        }
    }
}
